package com.yenlien.traceyun.milk.service;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.utils.EasyPoiUtils;
import com.yenlien.traceyun.milk.dao.FarmersDao;
import com.yenlien.traceyun.milk.dao.MilkBucketDao;
import com.yenlien.traceyun.milk.entity.MilkBucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Date 2022/7/11
 * @Author jiaLong
 * @Descript
 */
@Service
public class MilkBucketService {

    @Autowired
    private MilkBucketDao milkBucketDao;
    @Autowired
    private FarmersDao farmersDao;

    public JSONObject count(String companyId){
        JSONObject object = new JSONObject();
        object.put("rfid", milkBucketDao.count(companyId));
        object.put("farmer", farmersDao.count(companyId));
        return object;
    }


    public MilkBucket save(MilkBucket milkBucket) {
        return milkBucketDao.save(milkBucket);
    }

    public Page<MilkBucket> findByList(MilkBucket milkBucket) {
        Pageable pageable = PageRequest.of(milkBucket.getPage(), milkBucket.getPageSize(), Sort.Direction.DESC, "createtime");
        return milkBucketDao.findAll((Root<MilkBucket> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
            List<Predicate> list = new ArrayList<>();
            if (!StringUtils.isEmpty(milkBucket.getCompanyId())) {
                list.add(cb.equal(root.get("companyId"), milkBucket.getCompanyId()));
            }
            if (!StringUtils.isEmpty(milkBucket.getIdentifier())) {
                list.add(cb.equal(root.get("identifier"), milkBucket.getIdentifier()));
            }
            if (!StringUtils.isEmpty(milkBucket.getState())) {
                list.add(cb.equal(root.get("state"), milkBucket.getState()));
            }
            if (!StringUtils.isEmpty(milkBucket.getIsUsed())) {
                list.add(cb.equal(root.get("isUsed"), milkBucket.getIsUsed()));
            }
            if (!StringUtils.isEmpty(milkBucket.getCapacity())) {
                list.add(cb.equal(root.get("capacity"), milkBucket.getCapacity()));
            }
            Predicate[] predicate = new Predicate[list.size()];
            return cb.and(list.toArray(predicate));
        }, pageable);
    }


    public void importMilk(MultipartFile file, String companyId, String userId) {
        List<MilkBucket> milkBucketList = null;
        try {
            milkBucketList = EasyPoiUtils.importExcel(file, 1, 2, MilkBucket.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(CollectionUtils.isEmpty(milkBucketList)){
           return;
        }
        for(MilkBucket milkBucket : milkBucketList){
            milkBucket.setCompanyId(companyId);
            milkBucket.setUserId(userId);
            milkBucket.setCreatetime(new Date());
            milkBucket.setState("1");
            milkBucket.setIsUsed("2");
        }
        milkBucketDao.saveAll(milkBucketList);
    }





}
